package com.uvnos4j.matilda.commons.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.uvnos4j.matilda.commons.domain.IdDomain;
import lombok.Data;

/**
 * Vo 抽象类
 * <p>
 * Description:Vo 抽象类，提供根据Vo对象构建Domain对象的方法。
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.commons.vo
 * @since 2020-08-31 22:21:39
 */
@Data
public abstract class AbstractVo<T extends IdDomain> {

    protected Long id;

    /**
     * 构造实体
     *
     * @param needsId 是否需要添加ID
     * @return vo
     */
    public T buildModel(Class<T> clazz, boolean needsId) {
        try {
            T t = clazz.newInstance();
            CopyOptions options = new CopyOptions();
            options.ignoreNullValue();
            options.setIgnoreProperties("id");
            BeanUtil.copyProperties(this, t, options);
            if (needsId) {
                t.setId(id);
            }
            return t;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}